//
//  NSNumber+SCFloat.m
//  Shuaiche
//
//  Created by dangshuai on 2019/1/21.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "NSNumber+SCFloat.h"

@implementation NSNumber (SCFloat)

- (NSString *)floatString {
    CGFloat f = self.floatValue;
    if (f == 0) {
        return @"";
    }
    NSDecimalNumber *v = [[NSDecimalNumber decimalNumberWithDecimal:self.decimalValue] decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:@"10000"]];
    NSString *stringValue = v.stringValue;
    NSRange range = [stringValue rangeOfString:@"."];
    if (range.location == NSNotFound) {
        return [NSString stringWithFormat:@"%.2f",v.floatValue];
    } else {
        if (range.location + 3 <= stringValue.length) {
            return [stringValue substringToIndex:range.location + 3];
        }
    }
    
    return stringValue;
}

- (NSString *)floatStringWithSuffix:(NSString *)suffix {
    CGFloat f = self.floatValue;
    return [NSString stringWithFormat:@"%.2f%@",f/10000.f,suffix];
}

- (NSString *)intStringGreateWithSuffix:(NSString *)suffix {
    CGFloat f = self.floatValue;
    if (f > 100000000) {
        NSInteger a = (NSInteger)(f / 10000);
        return [NSString stringWithFormat:@"%zd%@",a,suffix];
    }
    return [NSString stringWithFormat:@"%.2f%@",f/10000.f,suffix];
}

- (NSString *)floatStringWithPrefix:(NSString *)pre suffix:(NSString *)suffix {
    CGFloat f = self.floatValue;
    return [NSString stringWithFormat:@"%@%.2f%@",pre,f/10000.f,suffix];
}

- (NSString *)floatStringWithDefault:(NSString *)fault {
    CGFloat f = self.floatValue;
    if (f > 0) {
        return [NSString stringWithFormat:@"%.2f",f/10000.f];
    }
    return fault;
}

- (NSString *)floatStringWithNoZeroTail {
    
    NSString *string = [self floatString];
    if ([string hasSuffix:@".00"]) {
        string = [string stringByReplacingOccurrencesOfString:@".00" withString:@""];
    }
    return string;
}

+ (NSNumber *)decimalNumberByMultiplyingString:(NSString *)string {
    NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:string];
    return [decimal decimalNumberByMultiplyingByPowerOf10:4];
}

- (NSNumber *)multipyByPowerOf10:(short)power {
    NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithDecimal:self.decimalValue];
    return [decimal decimalNumberByMultiplyingByPowerOf10:power];
}

- (NSString *)intString {
    return [NSString stringWithFormat:@"%zd",self.integerValue];
}

@end
